package com.xmsx.back.service.admin;

import com.xmsx.back.mapper.admin.GraduateMapper;
import com.xmsx.back.pojo.Graduate;
import com.xmsx.back.results.Result;
import com.xmsx.back.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GraduateService {
    @Autowired
    GraduateMapper graduateMapper;

    /**
     * 根据考生号获得毕业信息
     *
     * @param id
     * @return
     */
    public Graduate getById(String id) {
        return graduateMapper.getById(id);
    }

    /**
     * 根据学号获得毕业信息
     *
     * @param sid
     * @return
     */
    public Graduate getBySid(String sid) {
        return graduateMapper.getById(sid);
    }

    /**
     * 添加单条信息
     *
     * @param graduate
     */
    public void setGraduate(Graduate graduate) {
        graduateMapper.setGraduate(graduate);
    }

    /**
     * 批量添加信息 开启事务
     */
    @Transactional
    public Result<String> batchInsert(List<Graduate> graduateList) {
        try {
            // try insert
            for (Graduate graduate : graduateList) {
                Graduate graduate0 = getById(graduate.getId());
                if (null != graduate0) {
                    // 已经存在，报错
                    throw new Exception();
                }
                setGraduate(graduate);
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return Result.success("添加成功！");
    }

    /**
     * 修改单条信息
     *
     * @param graduate
     */
    public void updateGraduate(Graduate graduate) {
        graduateMapper.updateGraduate(graduate);
    }

    /**
     * 删除单条信息
     *
     * @param id
     */
    public void deleteGraduate(String id) {
        graduateMapper.deleteGraduate(id);
    }

    /**
     * 分页查找多条数据
     *
     * @param page
     * @param size
     * @return
     */
    public Pager<Graduate> findByPager(int page, int size, int start, int end, String edu) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", (page - 1) * size);
        params.put("size", size);
        if (start != 0 && end != 0 && edu == ""){
            params.put("start", start);
            params.put("end", end);
            Pager<Graduate> pager = new Pager<Graduate>();
            List<Graduate> list = graduateMapper.findByYearByPager(params);
            pager.setRows(list);
            pager.setTotal(graduateMapper.countByYear(start, end));
            return pager;
        }else if (start == 0 && end == 0 && edu != ""){
            params.put("edu", edu);
            Pager<Graduate> pager = new Pager<Graduate>();
            List<Graduate> list = graduateMapper.findByEduByPager(params);
            pager.setRows(list);
            pager.setTotal(graduateMapper.countByEdu(edu));
            return pager;
        }else if (start != 0 && end != 0 && edu != ""){
            params.put("start", start);
            params.put("end", end);
            params.put("edu", edu);
            Pager<Graduate> pager = new Pager<Graduate>();
            List<Graduate> list = graduateMapper.findByYearAndEduByPager(params);
            pager.setRows(list);
            pager.setTotal(graduateMapper.countByYearAndEdu(start, end, edu));
            return pager;
        }else {
            Pager<Graduate> pager = new Pager<Graduate>();
            List<Graduate> list = graduateMapper.findByPager(params);
            pager.setRows(list);
            pager.setTotal(graduateMapper.count());
            return pager;
        }
    }

    public List<Integer> getYearList() {
        return graduateMapper.getYearList();
    }
}
